package com.lb.shopguide.util.lb;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.lb.shopguide.entity.GuidePerformanceBean;
import com.lb.shopguide.entity.local.InOutData;
import com.lb.shopguide.entity.local.LineBean;
import com.lb.shopguide.entity.local.PieBean;
import com.lb.shopguide.ui.view.chart.LbBarView;
import com.lb.shopguide.ui.view.chart.LbCircleView;
import com.lb.shopguide.ui.view.chart.LbLineView;
import com.lb.shopguide.ui.view.chart.LbTwoLineView;
import com.lb.shopguide.ui.view.chart.entity.MemberPercentBean;
import com.lb.shopguide.ui.view.chart.entity.PerformanceBean;
import com.lb.shopguide.ui.view.chart.entity.SeriesDataBean;
import com.lb.shopguide.ui.view.chart.entity.ShopDataBean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by kang on 2018/1/10.
 */

public class ChartUtil {


    public static void bindLineChart(LbLineView lineView, PerformanceBean performanceBean) {
        ArrayList<String> xData = performanceBean.getxAxis().get(0).getData();
        ArrayList<LineBean> list = new ArrayList<>();

        ArrayList<SeriesDataBean> yData = performanceBean.getSeries().get(0).getData();
        Float maxData = 0.0f;
        for (int i = 0; i < xData.size(); i++) {
//            float key = Float.parseFloat(xData.get(i));//横坐标不要搞特殊的
            float value = 0.0f;
            if (TextUtils.isEmpty(yData.get(i).getValue())) {
                LineBean bean = new LineBean();
                value = 0.0f;
                maxData = (value > maxData) ? value : maxData;
                bean.setDate(xData.get(i));
                bean.setValue(value);
                list.add(bean);
            } else {
                LineBean bean = new LineBean();
                value = Float.parseFloat(yData.get(i).getValue());
                maxData = (value > maxData) ? value : maxData;
                bean.setDate(xData.get(i));
                bean.setValue(value);
                list.add(bean);
            }
        }
        lineView.setData(list, maxData);
    }

    public static void bindLineChartGuide(LbTwoLineView lineView, GuidePerformanceBean performanceBean) {
        ArrayList<LineBean> listOrder = new ArrayList<>();
        ArrayList<LineBean> listMember = new ArrayList<>();
        float maxData = 0.0f;
        float value = 0.0f;
        JsonArray jsonArrayOrder = performanceBean.getMonthsOrderList();
        for (JsonElement jsonObject : jsonArrayOrder) {
            Map mapTypes = JSON.parseObject(jsonObject.toString());
            Set keySet = mapTypes.keySet();
            Iterator iterator = keySet.iterator();
            while (iterator.hasNext()) {
                LineBean bean = new LineBean();
                Object key = iterator.next();
                bean.setDate("" + key);
                value = Float.parseFloat(mapTypes.get(key).toString());
                bean.setValue(value);
                maxData = (value > maxData) ? value : maxData;
                listOrder.add(bean);
            }
        }
        JsonArray jsonArrayMember = performanceBean.getMonthsMemberList();
        if (jsonArrayMember != null) {
            for (JsonElement jsonObject : jsonArrayMember) {
                Map mapTypes = JSON.parseObject(jsonObject.toString());
                Set keySet = mapTypes.keySet();
                Iterator iterator = keySet.iterator();
                while (iterator.hasNext()) {
                    LineBean bean = new LineBean();
                    Object key = iterator.next();
                    bean.setDate("" + key);
                    value = Float.parseFloat(mapTypes.get(key).toString());
                    bean.setValue(value);
                    maxData = (value > maxData) ? value : maxData;
                    listMember.add(bean);
                }
            }
        }

//        ArrayList<SeriesDataBean> yData = performanceBean.getSeries().get(0).getData();

//        for (int i = 0; i < xData.size(); i++) {
////            float key = Float.parseFloat(xData.get(i));//横坐标不要搞特殊的
//            float value = 0.0f;
//            if (TextUtils.isEmpty(yData.get(i).getValue())) {
//            } else {
//                LineBean bean = new LineBean();
//                value = Float.parseFloat(yData.get(i).getValue());
//                maxData = (value > maxData) ? value : maxData;
//                bean.setDate(xData.get(i));
//                bean.setValue(value);
//                list.add(bean);
//            }
//        }
        lineView.setData(listMember, listOrder, maxData);
    }

    public static boolean bindPieChartInHome(LbCircleView pieChart, PerformanceBean performanceBean, int[] colors) {
        ArrayList<SeriesDataBean> seriesDatas = performanceBean.getSeries().get(0).getData();
        ArrayList<PieBean> list = new ArrayList<>();
        boolean flag = false;
        for (int i = 0; i < seriesDatas.size(); i++) {
            PieBean pieBean = new PieBean();
            SeriesDataBean bean = seriesDatas.get(i);
            if (!bean.getScaleValue().equals("0.00%")) {//绘制不出这个区域，不要这个数据
                pieBean.setColor(colors[i]);
                pieBean.setTopText(bean.getValue() + "人");
                pieBean.setBottomText(bean.getScaleValue());
                String perStr = BizUtil.getPureNumber(bean.getScaleValue());
                float percent = Float.parseFloat(perStr) * 360 / 100;
                pieBean.setValue(percent);
                flag = true;
            } else {
                continue;
            }
            list.add(pieBean);
        }
        pieChart.setData(list);
        return flag;
    }

    public static boolean bindPieChartInShop(LbCircleView pieChart, PerformanceBean performanceBean, int[] colors) {
        ArrayList<SeriesDataBean> seriesDatas = performanceBean.getSeries().get(0).getData();
        ArrayList<PieBean> list = new ArrayList<>();
        boolean flag = false;
        for (int i = 0; i < seriesDatas.size(); i++) {
            PieBean pieBean = new PieBean();
            SeriesDataBean bean = seriesDatas.get(i);
            if (!bean.getScaleValue().equals("0.00%")) {//绘制不出这个区域，不要这个数据
                pieBean.setColor(colors[i]);
                pieBean.setTopText(bean.getName());
                pieBean.setBottomText(bean.getScaleValue());
                String perStr = BizUtil.getPureNumber(bean.getScaleValue());
                float percent = Float.parseFloat(perStr) * 360 / 100;
                pieBean.setValue(Math.round(percent));
                flag = true;
            } else {
                continue;
            }
            list.add(pieBean);
        }
        pieChart.setData(list);
        return flag;
    }


    public static void bindBarChart(LbBarView drawBaseLine, PerformanceBean performanceBean) {
        ArrayList<SeriesDataBean> incomeData = performanceBean.getSeries().get(0).getData();
        ArrayList<SeriesDataBean> outcomeData = performanceBean.getSeries().get(1).getData();
        ArrayList<String> xData = performanceBean.getxAxis().get(0).getData();

        ArrayList<InOutData> list = new ArrayList<>();
        Float maxData = 0.0f;
        for (int i = 0; i < incomeData.size(); i++) {
            InOutData data = new InOutData();
            float income = Float.parseFloat(incomeData.get(i).getValue());
            maxData = (income > maxData) ? income : maxData;
            float outcome = Float.parseFloat(outcomeData.get(i).getValue());
            maxData = (outcome > maxData) ? outcome : maxData;
            data.setInData(income);
            data.setOutData(outcome);
            data.setDate(xData.get(i));
            list.add(data);
        }
        drawBaseLine.setData(list, maxData);
    }

    public static ArrayList<ShopDataBean> getShopDataList(PerformanceBean performanceBean) {
        ArrayList<ShopDataBean> list = new ArrayList<>();
        ArrayList<SeriesDataBean> seriesDataBeans = performanceBean.getSeries().get(0).getData();
        ArrayList<String> shopNames = performanceBean.getyAxis().get(0).getData();
        for (int i = 0; i < seriesDataBeans.size(); i++) {
            ShopDataBean bean = new ShopDataBean();
            bean.setShopName(shopNames.get(i));
            bean.setCurSale(seriesDataBeans.get(i).getValue());
            bean.setCurPer(seriesDataBeans.get(i).getScaleValue());
            list.add(bean);
        }
        return list;
    }


    public static ArrayList<MemberPercentBean> getMemberPercentList(PerformanceBean performanceBean) {
        ArrayList<MemberPercentBean> list = new ArrayList<>();
        ArrayList<SeriesDataBean> seriesDataBeans = performanceBean.getSeries().get(0).getData();
        ArrayList<String> shopNames = performanceBean.getyAxis().get(0).getData();
        if (seriesDataBeans != null) {
            for (int i = 0; i < seriesDataBeans.size(); i++) {
                MemberPercentBean bean = new MemberPercentBean();
                SeriesDataBean seriesDataBean = seriesDataBeans.get(i);
                bean.setCurPer(seriesDataBean.getScaleValue());
                bean.setMemberNum(seriesDataBean.getValue());
                bean.setShopName(shopNames.get(i));
                list.add(bean);
            }
        }
        return list;
    }
}
